package kgc.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import kgc.crm.mapper.AdminRecordMapper;
import kgc.crm.model.AdminRecord;
import kgc.crm.model.Leads;
import kgc.crm.service.IAdminRecord;
import kgc.crm.util.Constant;
import kgc.crm.util.EmptyChecker;
import kgc.crm.util.JWTManager;
import kgc.crm.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AdminRecordImpl implements IAdminRecord {
    @Autowired
    private AdminRecordMapper recordMapper;
    @Override
    public ResponseEntity add(AdminRecord record,String apiToken) {
        //添加对象不能为空
        if (EmptyChecker.isEmpty(record)) {
            log.error("【添加跟进信息】：{},添加跟进记录不能为空]",record);
            return ResultUtil.error("添加失败，对象不能为空");
        }
        //绑定当前登录的用户ID
        Long id = JWTManager.getUserIDByToken(apiToken);
        record.setCreateUserId(id);
        //指定types类型 把leads线索类型传进去
        record.setTypes(Constant.LEADS_TYPE_NAME);
        int insert = -1;
        try {
            insert = recordMapper.insert(record);
        } catch (Exception e) {
            log.error("【添加跟进信息】添加失败，sql执行错误：{}",e);
        }
        if (insert <= 0)
            return ResultUtil.error("【添加跟进信息】,添加失败");
        return ResultUtil.success("【添加跟进信息】,添加成功");
    }

    @Override
    public ResponseEntity upd(AdminRecord record) {
        if (EmptyChecker.isEmpty(record)) {
            log.error("【修改跟进信息】：{},修改跟进记录不能为空]",record);
            return ResultUtil.error("修改跟进信息失败");
        }
        QueryWrapper<AdminRecord> wrapper = new QueryWrapper<>();
        wrapper.eq(AdminRecord.COL_RECORD_ID,record.getRecordId());
        int update= -1;
        try {
            update = recordMapper.update(record, wrapper);
        } catch (Exception e) {
            log.error("【修改跟进信息】修改失败：{}，sql有误",e);
        }
        if (update <= 0) {
            return ResultUtil.error("【修改跟进信息】,修改失败");
        }
        return ResultUtil.success("【修改跟进信息】,修改成功");
    }

    @Override
    public ResponseEntity del(Integer id) {
        if (EmptyChecker.isEmpty(id)) {
            log.error("【删除跟进信息】：{},id不能为空]",id);
            return ResultUtil.error("删除跟进信息失败");
        }
        int i = -1;
        try {
            i = recordMapper.deleteById(id);
        } catch (Exception e) {
            log.error("【删除跟进信息】删除失败：{}，sql有误",e);
        }
        if (i <= 0) {
            return ResultUtil.error("删除跟进信息失败");
        }
        return ResultUtil.success("删除跟进信息成功");
    }

    @Override
    public ResponseEntity sel(Integer id,String apiToken) {
        if (EmptyChecker.isEmpty(id)) {
            log.error("【查询跟进信息】查询失败：{},id不能为空]",id);
            return ResultUtil.error("查询跟进信息失败");
        }
        //获取当前用户id
        Long userId = JWTManager.getUserIDByToken(apiToken);
        QueryWrapper<AdminRecord> wrapper = new QueryWrapper<>();
        wrapper.eq(AdminRecord.COL_TYPES,Constant.LEADS_TYPE_NAME)
                .eq(AdminRecord.COL_TYPES_ID,id)
                .eq(AdminRecord.COL_CREATE_USER_ID,userId)
                .orderByDesc(AdminRecord.COL_CREATE_TIME);
        List<AdminRecord> list = recordMapper.selectList(wrapper);
        return ResultUtil.success(list);
    }
}
